local multi_child_view = require 'views.multi_child_view'
local vec2 = require 'vec2'
local constrained_size = require 'constrained_size'

---@class stack: multi_child_view
---@overload fun(options: {children: view[]}): stack
---@diagnostic disable-next-line: assign-type-mismatch
local stack = multi_child_view:new_view_type()

---@class stack.renderer: multi_child_view.renderer
stack.renderer = multi_child_view.renderer:new_renderer_type()

---@param options { children: view[] }
---@return stack
function stack:__call(options)
    self:set_view_prototype(options)

    options.children = options.children or {}

    return options
end

---@param constraints constraints
---@param view stack
function stack.renderer:layout(constraints, view)
    local overall_size = constrained_size.unconstrained()
    local child_count = #self.child_renderers

    for i = 1, child_count do
        local child_size = self.child_renderers[i]:layout(constraints, view.children[i])
        if not child_size.width_unconstrained then
            overall_size.width = overall_size.width_unconstrained
                and child_size.width
                or overall_size.width + child_size.width
        end

        if not child_size.height_unconstrained then
            overall_size.height = overall_size.height_unconstrained
                and child_size.height
                or overall_size.height + child_size.height
        end
    end

    return overall_size
end

return stack
